#ifndef TABLEDELEGATES_H
#define TABLEDELEGATES_H

#include <QObject>
#include <QStyledItemDelegate>
#include <QComboBox>
#include <QAbstractItemModel>
#include <QWidget>
#include <QStyleOptionViewItem>
#include <QDateEdit>
#include <QLabel>
#include <QPixmap>
#include <QPainter>
#include <QMouseEvent>
#include <QFileDialog>
#include <QStandardPaths>
#include <QBuffer>
#include <QEvent>
#include <QDebug>

class ComboBoxDelegate : public QStyledItemDelegate
{
    Q_OBJECT

public:
    explicit ComboBoxDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}
    //设置下拉菜单的选项
    void setItems(const QStringList &items) { m_items = items; }
    //创建编辑器
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
    {
        Q_UNUSED(option)
        Q_UNUSED(index)

        QComboBox *comboBox = new QComboBox(parent);
        comboBox->addItems(m_items);
        return comboBox;
    }
    //设置模型数据
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
    {
        QComboBox *comboBox = static_cast<QComboBox *>(editor);
        if (comboBox) {
            model->setData(index, comboBox->currentText().trimmed(), Qt::EditRole);
        }
    }
private:
    QStringList m_items;  //存储下拉菜单的选项
};

//日历选择器
class DateEditDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    explicit DateEditDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}
    //创建编辑器
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
    {
        Q_UNUSED(option)
        Q_UNUSED(index)

        QDateEdit *dateEdit = new QDateEdit(parent);
        dateEdit->setDisplayFormat("yyyy-MM-dd");  //设置日期格式
        dateEdit->setCalendarPopup(true);  //设置日历弹出窗口
        return dateEdit;
    }
    //设置模型数据
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
    {
        QDateEdit *dateEdit = qobject_cast<QDateEdit *>(editor);
        if (dateEdit) {
            model->setData(index, dateEdit->date().toString("yyyy-MM-dd"), Qt::EditRole);
        }
    }
};

//选择图片选择器
class PhotoDelegate : public QStyledItemDelegate
{
    Q_OBJECT
public:
    explicit PhotoDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {}
    //创建编辑器
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override
    {
        Q_UNUSED(option)
        Q_UNUSED(index)

        QLabel *label = new QLabel(parent);
        label->setMinimumSize(100, 100);  //设置最小尺寸
        label->setScaledContents(true);  //设置缩放内容
        return label;
    }
    //设置模型数据
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override
    {
        QLabel *label = qobject_cast<QLabel *>(editor);
        if (label) {
            //将图片转换为字节数据
            QByteArray imageData;
            const QPixmap *pixmap = label->pixmap();
            if (!pixmap->isNull()) {
                QBuffer buffer(&imageData);
                buffer.open(QIODevice::WriteOnly);
                pixmap->save(&buffer, "PNG");  //保存为PNG格式
                buffer.close();  //关闭缓冲区
            }
            model->setData(index, imageData, Qt::UserRole);  //将图片数据存储到模型中
        }
    }
    //绘制单元格
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
    {
        QByteArray imageData = index.data(Qt::UserRole).toByteArray();  //获取图片数据

        if (imageData.isEmpty()) {
            QStyledItemDelegate::paint(painter, option, index);  //如果没有图片，则使用默认绘制
            return;
        }
        QPixmap pixmap;
        pixmap.loadFromData(imageData);  //加载图片数据
        if (pixmap.isNull()) {
            QStyledItemDelegate::paint(painter, option, index);  //如果加载失败，则使用默认绘制
            return;
        }
        //绘制图片
        QRect rect = option.rect;
        QPixmap scaledPixmap = pixmap.scaled(100, 100, Qt::KeepAspectRatio);  //缩放图片
        painter->drawPixmap(rect, scaledPixmap);  //绘制图片
    }
    //editor事件
    bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index) override
    {
        QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(event);
        if (event->type() != QEvent::MouseButtonDblClick || mouseEvent->button() != Qt::LeftButton) {  //非左键双击事件
            return QStyledItemDelegate::editorEvent(event, model, option, index);  //如果不是双击事件，则使用默认处理
        }
        //处理鼠标点击事件

        //打开文件选择对话框
        QString fileName = QFileDialog::getOpenFileName(nullptr, tr("Select Photo"), QStandardPaths::writableLocation(QStandardPaths::PicturesLocation), tr("Images (*.png *.jpg *.bmp)"));
        if (fileName.isEmpty()) {
            return true;
        }
        //加载图片
        QFile file(fileName);
        if (!file.open(QIODevice::ReadOnly)) {
            return true;
        }
        QByteArray imageData = file.readAll();  //读取文件数据
        file.close();  //关闭文件
        //将图片数据存储到模型中
        model->setData(index, imageData, Qt::UserRole);  //存储图片数据
        
        return true;
    }
};

#endif // TABLEDELEGATES_H